<?php

/**
 * This is the model class for table "gl".
 *
 * The followings are the available columns in table 'gl':
 * @property string $id
 * @property string $treeLevel
 * @property string $parentUid
 * @property string $glType
 * @property string $codePrefix
 * @property string $codePostfix
 * @property string $title
 * @property string $telephone
 *
 * The followings are the available model relations:
 * @property Gl $parentU
 * @property Gl[] $gls
 * @property Gltype $glType0
 * @property Salesjournal[] $salesjournals
 * @property Salesjournal[] $salesjournals1
 */
class Gl extends CActiveRecord
{
	const GL_ID_SALES = 29;
	const GL_ID_PURCHASE = 70;
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'gl';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('treeLevel, glType, codePrefix, codePostfix, title, telephone', 'required'),
			array('treeLevel, glType', 'length', 'max'=>5),
			array('parentUid', 'length', 'max'=>11),
			array('codePrefix', 'length', 'max'=>2),
			array('codePostfix', 'length', 'max'=>7),
			array('title', 'length', 'max'=>200),
			array('telephone', 'length', 'max'=>50),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, treeLevel, parentUid, glType, codePrefix, codePostfix, title, telephone', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'parentU' => array(self::BELONGS_TO, 'Gl', 'parentUid'),
			'gls' => array(self::HAS_MANY, 'Gl', 'parentUid'),
			'glType0' => array(self::BELONGS_TO, 'Gltype', 'glType'),
			'salesjournals' => array(self::HAS_MANY, 'Salesjournal', 'accountReceivableGlId'),
			'salesjournals1' => array(self::HAS_MANY, 'Salesjournal', 'customerGlId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'treeLevel' => 'Tree Level',
			'parentUid' => 'Parent Uid',
			'glType' => 'Gl Type',
			'codePrefix' => 'Code Prefix',
			'codePostfix' => 'Code Postfix',
			'title' => 'Title',
			'telephone' => 'Telephone',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('treeLevel',$this->treeLevel,true);
		$criteria->compare('parentUid',$this->parentUid,true);
		$criteria->compare('glType',$this->glType,true);
		$criteria->compare('codePrefix',$this->codePrefix,true);
		$criteria->compare('codePostfix',$this->codePostfix,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('telephone',$this->telephone,true);
		$criteria->order = 'glType, codePrefix, codePostfix ASC';
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Gl the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	/**
	 * Return formated dropdownlist.
	 * @return Gl List
	 */
	public static function getDropdownList($condition)
	{
		$list = Gl::model()->findAll(array('condition'=>$condition, 'order'=>'glType, codePrefix, codePostfix, title'));
		foreach ($list as $item)
		{
			$item->title = str_repeat('-', $item->treeLevel).' '.$item->codePrefix.' '.$item->codePostfix.' '.$item->title; 
		}
		return $list;
	}
}
